Page({
  data: {
    active: 0,
    orderList: [],
    loading: false,
    finished: false
  },

  onLoad(options) {
    // 设置选中标签
    if (options.type) {
      const typeMap = {
        'unpaid': 0,
        'undelivered': 1,
        'delivered': 2,
        'completed': 3
      };
      this.setData({
        active: typeMap[options.type] || 0
      });
    }
    
    // 加载订单列表
    this.loadOrders();
  },

  // 切换标签
  onChange(event) {
    this.setData({
      active: event.detail.index,
      orderList: [],
      finished: false
    });
    this.loadOrders();
  },

  // 加载订单列表
  async loadOrders() {
    if (this.data.loading || this.data.finished) return;
    
    this.setData({ loading: true });
    
    try {
      // TODO: 调用订单列表接口
      const status = ['unpaid', 'undelivered', 'delivered', 'completed'][this.data.active];
      // const res = await getOrders({ status });
      const res = {
        data: []
      };
      
      this.setData({
        orderList: [...this.data.orderList, ...res.data],
        finished: res.data.length < 10
      });
    } catch (error) {
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 取消订单
  cancelOrder(event) {
    const { id } = event.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: '确定要取消该订单吗？',
      success: async (res) => {
        if (res.confirm) {
          try {
            // TODO: 调用取消订单接口
            // await cancelOrder(id);
            wx.showToast({
              title: '取消成功',
              icon: 'success'
            });
            // 刷新订单列表
            this.setData({
              orderList: [],
              finished: false
            });
            this.loadOrders();
          } catch (error) {
            wx.showToast({
              title: '取消失败',
              icon: 'none'
            });
          }
        }
      }
    });
  },

  // 支付订单
  payOrder(event) {
    const { id } = event.currentTarget.dataset;
    wx.showLoading({
      title: '支付中...'
    });
    
    // TODO: 调用支付接口
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '支付成功',
        icon: 'success'
      });
      // 刷新订单列表
      this.setData({
        orderList: [],
        finished: false
      });
      this.loadOrders();
    }, 1500);
  },

  // 确认收货
  confirmOrder(event) {
    const { id } = event.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: '确认已收到商品？',
      success: async (res) => {
        if (res.confirm) {
          try {
            // TODO: 调用确认收货接口
            // await confirmOrder(id);
            wx.showToast({
              title: '确认成功',
              icon: 'success'
            });
            // 刷新订单列表
            this.setData({
              orderList: [],
              finished: false
            });
            this.loadOrders();
          } catch (error) {
            wx.showToast({
              title: '确认失败',
              icon: 'none'
            });
          }
        }
      }
    });
  }
}); 